package com.androturka.billintur.cyberpark;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.androturka.cyberpark.R;


class MenuAdapter extends BaseAdapter {

	private DailyMenu[] menus;

	public MenuAdapter(Context context, DailyMenu[] menus) {
		mContext = context;
		this.menus = menus;
	}

	@Override
	public int getCount() {
		return this.menus.length;
	}

	@Override
	public Object getItem(int position) {
		return menus[position];
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = null;

		view = (View) LayoutInflater.from(mContext).inflate(R.layout.list_item,
					parent, false);
		if ( view!= null ) {
			ImageView collapseView = ( ImageView )view.findViewById(R.id.collapseView); 
			TextView tarihView = (TextView) view.findViewById(R.id.tarih);
			//Button tarihView = (Button) view.findViewById(R.id.tarih);
			TextView corbaView = (TextView) view.findViewById(R.id.corba);
			TextView anayemek1View = (TextView) view.findViewById(R.id.anayemek1);
			TextView anayemek2View = (TextView) view.findViewById(R.id.anayemek2);
			TextView vezeteryanView = (TextView) view.findViewById(R.id.vezeteryan);
			TextView pilavView = (TextView) view.findViewById(R.id.pilav);
			TextView makarnaborekView = (TextView) view.findViewById(R.id.makarnaborek);
			TextView zeytinyagliView = (TextView) view.findViewById(R.id.zeytinyagli);
			TextView tatli1View = (TextView) view.findViewById(R.id.tatli1);
			TextView tatli2View = (TextView) view.findViewById(R.id.tatli2);
			TextView meyveView = (TextView) view.findViewById(R.id.meyve);
			TextView salataView = (TextView) view.findViewById(R.id.salata);

			if ( menus[position] != null ) {
				
				//tarihView.setText(menus[position].getTarih());
				try {
					tarihView.setText(changeDateFormat(menus[position].getTarih()));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				corbaView.setText(menus[position].getCorba());
				anayemek1View.setText(menus[position].getAnayemek1());
				anayemek2View.setText(menus[position].getAnayemek2());
				vezeteryanView.setText(menus[position].getVejeteryan());
				pilavView.setText(menus[position].getPilav());
				makarnaborekView.setText(menus[position].getMakarnaborek());
				zeytinyagliView.setText(menus[position].getZeytinyagli());
				tatli1View.setText(menus[position].getTatli1());
				tatli2View.setText(menus[position].getTatli2());
				meyveView.setText(menus[position].getMeyve());
				salataView.setText(menus[position].getSalata());
				
				if ( getCount() != 1 )
					collapseView.setVisibility(View.VISIBLE);
				
				if ( ( menus[position].getCorba() == null ) || 
				     ( menus[position].getCorba().equals("null") ) ){
					TextView noteView = (TextView) view.findViewById(R.id.note);
					noteView.setVisibility(View.VISIBLE);
					
					if ( menus[position].getIsNextMonth() )
						noteView.setText("Henüz Bugüne Ait Menu Hazırlanmamıştır.");
					else
						noteView.setText("Bugün Yemek Servisi Yapılmamaktadır.");
					
					LinearLayout panelinfo = (LinearLayout) view.findViewById(R.id.panelinfo);
					panelinfo.setVisibility(View.GONE);				
				}
				

				LinearLayout viewHeader = (LinearLayout) view.findViewById(R.id.viewHeader);
				LinearLayout viewKap1 = (LinearLayout) view.findViewById(R.id.kap1);
				LinearLayout viewKap2 = (LinearLayout) view.findViewById(R.id.kap2);
				LinearLayout viewKap3 = (LinearLayout) view.findViewById(R.id.kap3);
				LinearLayout viewKap4 = (LinearLayout) view.findViewById(R.id.kap4);
				LinearLayout viewKap5 = (LinearLayout) view.findViewById(R.id.kap5);
				if ( menus[position].getIsToday() )
				{	
					//viewHeader.setBackgroundColor(0xFF63AFD0);
					viewHeader.setBackgroundColor(0xFF2E8AE6);
					viewKap1.setBackgroundColor(0xFF47A3FF);
					viewKap2.setBackgroundColor(0xFF70B8FF);
					viewKap3.setBackgroundColor(0xFF99CCFF);
					viewKap4.setBackgroundColor(0xFFC2E0FF);
					viewKap5.setBackgroundColor(0xFFEBF5FF);
				}
				else if ( menus[position].getIsUpcomingDay() )
				{
					//viewHeader.setBackgroundColor(0xFF0762A1);
					viewHeader.setBackgroundColor(0xFF007A00);
					viewKap1.setBackgroundColor(0xFF009900);
					viewKap2.setBackgroundColor(0xFF33AD33);
					viewKap3.setBackgroundColor(0xFF66C266);
					viewKap4.setBackgroundColor(0xFF99D699);
					viewKap5.setBackgroundColor(0xFFCCEBCC);					
				}
				else
				{
					//viewHeader.setBackgroundColor(0xFF00C322);
					/*
					viewHeader.setBackgroundColor(0xFFC20056);
					viewKap1.setBackgroundColor(0xFFDB1970);
					viewKap2.setBackgroundColor(0xFFE34D90);
					viewKap3.setBackgroundColor(0xFFEB80B0);
					viewKap4.setBackgroundColor(0xFFF3B2CF);
					viewKap5.setBackgroundColor(0xFFFBE6EF);
					*/
					viewHeader.setBackgroundColor(0xFFCC0000);
					viewKap1.setBackgroundColor(0xFFFF0000);
					viewKap2.setBackgroundColor(0xFFFF3333);
					viewKap3.setBackgroundColor(0xFFFF6666);
					viewKap4.setBackgroundColor(0xFFFF9999);
					viewKap5.setBackgroundColor(0xFFFFCCCC);
				}
			}

			if ( getCount() == 1 )
			{
				if ( menus[position] != null ) {					
					LinearLayout todayLayout = (LinearLayout) view.findViewById(R.id.panel);
					todayLayout.setVisibility(View.VISIBLE);
					ImageView image = (ImageView) view.findViewById(R.id.collapseView);
					image.setImageResource(R.drawable.collapse);
				}
				else {
					LinearLayout todayLayout = (LinearLayout) view.findViewById(R.id.panel);
					todayLayout.setVisibility(View.VISIBLE);

					TextView noteView = (TextView) view.findViewById(R.id.note);
					noteView.setVisibility(View.VISIBLE);				
					noteView.setText("Bugün Yemek Servisi Yapılmamaktadır");
					LinearLayout panelinfo = (LinearLayout) view.findViewById(R.id.panelinfo);
					panelinfo.setVisibility(View.GONE);									
				}
			}
		}
			
		//Log.d(Common.TAG,"position : " + position + " visibility : " + view.getVisibility());
		return view;
	}

	private Context mContext;

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public String changeDateFormat( String strDate ) throws ParseException
	{
	      SimpleDateFormat sdfSource = new SimpleDateFormat("dd.MM.yyyy");	     
	      Date date = sdfSource.parse(strDate);
	      SimpleDateFormat sdfDestination = new SimpleDateFormat("dd MMMM EEE");
	      strDate = sdfDestination.format(date);
	     
	      //System.out.println("Date is converted from dd/MM/yy format to MM-dd-yyyy hh:mm:ss");
	      //System.out.println("Converted date is : " + strDate);
	      
	      return strDate;
	}
}